<?php
namespace jy_newganfee;
defined('BY_JYA') or exit('error');
/*
	极差分红计算	
*/
class jicha extends \plugin{
	/*
		生成分红
	*/
	function create($order,$goods=array()){
		$this->order=$order;
		$this->goods=$goods;
		$this->commission_order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$this->order['mid'],
			'ordersn'=>$this->order['ordersn'],
			'order_price'=>$this->order['price'],
			'plugin_name'=>$this->order['plugin_name']?:'',
			'commission_money'=>0,
			'area_money'=>0,
			'xuni_money'=>0,
			'jifen'=>0,
			'red_money'=>0,
			'createtime'=>TIMESTAMP,
		);
		$member=$this->get('member',array('mid'=>$order['mid']));
		$nowlevel=$this->getcolumn('level',array('id'=>$member['level_id']),'level');
		//自己开始算起
		$fromuser=$member['mid'];
		$parameter=array(
			'params'=>array('uniacid'=>$_SESSION['uniacid']),
			'orderby'=>'level asc',
			'limit'=>'1',
		);		
		$levels=$this->get_list('level',$parameter);
		$minlevel=$levels[0]['level'];
		$parameter['orderby']='level desc';
		$levels=$this->get_list('level',$parameter);
		$this->commission_order['maxlevel']=$maxlevel=$levels[0]['level'];
		$levelnum=12;		
		//极差分红单线等级ABCDE，第一级没有分红，低于自己的没有分红，平级没有分红
		for($i=0;$i<$levelnum;$i++){
				if(!$fromuser){
					break;
				}
				$member=$this->get('member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
				$nextmember=$this->get('member',array('mid'=>$member['fromuser'],'uniacid'=>$_SESSION['uniacid']));
				$nextlevel=$this->getcolumn('level',array('id'=>$nextmember['level_id']),'level');
				$fromuser=$member['fromuser'];
				$level=$this->get('level',array('id'=>$member['level_id']));
				$level['level']=$level['level']?:0;
				//上等级不是最大级，当前等级小于上等级或小于最小等级，都没有分红	
				if($level['level']<$minlevel || $level['level']<$nowlevel){
					continue;
				}elseif($level['level']==$nextlevel){
					//跟上级平级分红
					$this->create_redcommission($member,$level,1);
				}else{
					//当等级匹配时，分红
					$this->create_redcommission($member,$level);
				}
				$nowlevel=$level['level'];
				if($fromuser==$member['mid']){
					break;
				}
		}
		return true;
	}
	/*
		级差分佣type=1;
	*/
	function create_redcommission($agent,$level=array(),$isping=0){
			if(!$agent['level_id']){
				//获取默认等级分红比例
				$level_commissions=$this->plugin_setting('default_level');
				$level_id=0;
				$commission_per=$level_commissions['ji_rate'];
				if($isping){
					$ping_commission_per=$level_commissions['pingfee'];
				}
			}else{
				$level['commissions']=json_decode($level['commissions'],true);
				$level_id=$level['id'];
				$commission_per=$level['commissions']['ji_rate'];
				if($isping){
					$ping_commission_per=$level['commissions']['pingfee'];
				}
			}
			$money=0;
			$order_price=$this->commission_order['order_price'];
			$remark=array(
				'level_id'=>$level_id,
				'order_price'=>$order_price,
				'goods_commission'=>array(),
			);
			$goods=$this->goods;
			$total_price=$goods['total_price'];
			if($goods['ignore'] || !$goods['id']){
				return false;
			}
			if($goods){
					//如果商品单独设置佣金，将减少本商品的金额
					$order_price=$order_price-$total_price;
					$goods['commissions']=$goods['commissions']?json_decode($goods['commissions'],true):array();
					foreach($goods['commissions'] as $gc){
						if($gc['level_id']==0){
							$demogoods_commission_per=$gc['ji_rate'];
							if($isping){
								$demogoods_ping_commission_per=$gc['pingfee'];
							}
						}
						if($gc['level_id']!=$level_id){
							continue;
						}else{
							$goods_commission_per=$gc['ji_rate'];
							if($isping){
								$goods_ping_commission_per=$gc['pingfee'];
							}
							break;
						}
					}
					$goods_commission_per=$goods_commission_per?:$demogoods_commission_per;
					$goods_commission_per=$goods_commission_per?:$commission_per;
					if($isping){
						$goods_ping_commission_per=$goods_ping_commission_per?:$demogoods_ping_commission_per;
						$ping_commission_per=$goods_ping_commission_per?:$ping_commission_per;
					}
					if(substr($goods_commission_per,-1)=='%'){
						$goods_commission_money=($total_price*substr($goods_commission_per,0,-1))/100;
					}else{
						$goods_commission_money=$goods_commission_per*$goods['total'];
					}
					$goods_commission_money -=$this->commission_order['commission_money'];
					$money+=$goods_commission_money;
			}
			$remark['commission_money']=$money;
			if($money>0){
				$this->commission_order['commission_money']+=$money;
				if($isping){
					if(substr($ping_commission_per,-1)=='%'){
						$shpingfee=($money*substr($ping_commission_per,0,-1))/100;
					}else{
						$shpingfee=$ping_commission_per;
					}
					if($shpingfee>0 && $agent['fromuser']>0){
						//分给上级
						$fufee=$this->model('model')->up_fee($shpingfee,$agent['fromuser']);
						$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$agent['fromuser'],
							'orderid'=>$this->order['id'],
							'money'=>$shpingfee,
							'fufee'=>$fufee,
							'realmoney'=>$shpingfee-$fufee,
							'createtime'=>TIMESTAMP,
							'status'=>1,
							'type'=>3,
						);
						//拿贡献奖
						$gongfee=$this->model('upfee')->create_shangjifee($new_commission,$agent['fromuser'],0,5);
						//拿感恩奖
						$nextfromuser=$this->getcolumn('member',array('mid'=>$agent['fromuser']),'fromuser');
						$ganfee=$this->model('upfee')->create_xiajifee($new_commission,$nextfromuser,0,4);
						$new_commission['gongfee']=$gongfee;
						$new_commission['ganfee']=$ganfee;
						$new_commission['realmoney']=$shpingfee-$fufee-$gongfee-$ganfee;
						$new_commission['realmoney']=$new_commission['realmoney']<0?0:$new_commission['realmoney'];
						$this->insert('commission',$new_commission);
					}
					$mypingfee=$money-$shpingfee;
					if($mypingfee>0){
						//分给自己
						$fufee=$this->model('model')->up_fee($mypingfee,$agent['mid']);
						$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$agent['mid'],
							'orderid'=>$this->order['id'],
							'money'=>$mypingfee,
							'fufee'=>$fufee,
							'realmoney'=>$mypingfee-$fufee,
							'createtime'=>TIMESTAMP,
							'status'=>1,
							'type'=>3,
						);
						//拿贡献奖
						$gongfee=$this->model('upfee')->create_shangjifee($new_commission,$agent['mid'],0,5);
						//拿感恩奖
						$ganfee=$this->model('upfee')->create_xiajifee($new_commission,$agent['fromuser'],0,4);
						$new_commission['gongfee']=$gongfee;
						$new_commission['ganfee']=$ganfee;
						$new_commission['realmoney']=$mypingfee-$fufee-$gongfee-$ganfee;
						$new_commission['realmoney']=$new_commission['realmoney']<0?0:$new_commission['realmoney'];
						$this->insert('commission',$new_commission);
					}
				}else{
					$fufee=$this->model('model')->up_fee($money,$agent['mid']);
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$agent['mid'],
						'orderid'=>$this->order['id'],
						'money'=>$money,
						'fufee'=>$fufee,
						'realmoney'=>$money-$fufee,
						'createtime'=>TIMESTAMP,
						'status'=>1,
						'type'=>2,
					);
					//拿贡献奖
					$gongfee=$this->model('upfee')->create_shangjifee($new_commission,$agent['mid'],0,5);
					//拿感恩奖
					$ganfee=$this->model('upfee')->create_xiajifee($new_commission,$agent['fromuser'],0,4);
					$new_commission['gongfee']=$gongfee;
					$new_commission['ganfee']=$ganfee;
					$new_commission['realmoney']=$money-$fufee-$gongfee-$ganfee;
					$new_commission['realmoney']=$new_commission['realmoney']<0?0:$new_commission['realmoney'];
					$this->insert('commission',$new_commission);
				}
			}
			//返回上级
			return $agent['fromuser'];
	}
}